﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using Tesla.Content;
using Tesla.Core;
using Tesla.Graphics;
using Tesla.Graphics.Implementation;
using Tesla.Xna.Core;
using Tesla.Xna.Graphics.Implementation;
using XFG = Microsoft.Xna.Framework.Graphics;
using XNA = Microsoft.Xna.Framework;

namespace Tesla.Xna.Graphics {
    public sealed class XNARenderSystemProvider : IRenderSystemProvider {
        private XFG.GraphicsDevice _device;
        internal XNAGame _game;
        private XNA.GraphicsDeviceManager _gdManager;
        private XNARenderer _renderer;
        private ContentManager _content;

         public IRenderer Renderer {
            get {
                return _renderer;
            }
        }

         public ContentManager DefaultContent {
             get {
                 return _content;
             }
         }

         public String Platform {
             get {
                 return "XNA_4_0";
             }
         }

         public IGraphicsAdapter DefaultAdapter {
             get { return new XNAGraphicsAdapter(_gdManager.GraphicsDevice, XFG.GraphicsAdapter.DefaultAdapter, 0);}
         }

         public Collections.ReadOnlyList<IGraphicsAdapter> Adapters {
             get { throw new NotImplementedException(); }
         }

        public XNARenderSystemProvider() {
            _game = new XNAGame();
            _gdManager = new XNA.GraphicsDeviceManager(_game);
            _gdManager.GraphicsProfile = XFG.GraphicsProfile.HiDef;
            _gdManager.ApplyChanges();
            _renderer = new XNARenderer(_gdManager.GraphicsDevice);
            _device = _renderer.GraphicsDevice;
            _content = new ContentManager(new EmbeddedResourceLocator(Tesla.Xna.DefaultContent.ResourceManager));
            _content.UseDefaultContent = false;
        }

        public SwapChainImplementation CreateSwapChainImplementation(IntPtr windowHandle, PresentationParameters presentParams) {
            return new XNASwapChainImplementation(_gdManager, windowHandle, presentParams);
        }

        public BlendStateImplementation CreateBlendStateImplementation() {
            return new XNABlendStateImplementation();
        }

        public DepthStencilStateImplementation CreateDepthStencilStateImplementation() {
            return new XNADepthStencilStateImplementation();
        }

        public SamplerStateImplementation CreateSamplerStateImplementation() {
            return new XNASamplerStateImplementation();
        }

        public RasterizerStateImplementation CreateRasterizerStateImplementation() {
            return new XNARasterizerStateImplementation();
        }

        public VertexBufferImplementation CreateVertexBufferImplementation(VertexDeclaration declaration, int vertexCount, ResourceUsage usage) {
            return new XNAVertexBufferImplementation(_renderer, declaration, vertexCount, usage);
        }

        public VertexBufferImplementation CreateVertexBufferImplementation(VertexDeclaration declaration, ResourceUsage usage, DataBuffer data) {
            return new XNAVertexBufferImplementation(_renderer, declaration, usage, data);
        }

        public VertexBufferImplementation CreateVertexBufferImplementation(VertexDeclaration declaration, ResourceUsage usage, params DataBuffer[] data) {
            return new XNAVertexBufferImplementation(_renderer, declaration, usage, data);
        }

        public IndexBufferImplementation CreateIndexBufferImplementation(IndexFormat format, int indexCount, ResourceUsage usage) {
            return new XNAIndexBufferImplementation(_renderer, format, indexCount, usage);
        }

        public IndexBufferImplementation CreateIndexBufferImplementation(DataBuffer<int> data, ResourceUsage usage) {
            return new XNAIndexBufferImplementation(_renderer, data, usage);
        }

        public IndexBufferImplementation CreateIndexBufferImplementation(DataBuffer<short> data, ResourceUsage usage) {
            return new XNAIndexBufferImplementation(_renderer, data, usage);
        }

        public EffectImplementation CreateEffectImplementation(byte[] shaderByteCode) {
            return new XNAEffectImplementation(_renderer, shaderByteCode);
        }

        public Texture1DImplementation CreateTexture1DImplementation(int width, bool genMipmap, SurfaceFormat format, DataBuffer data) {
            return new XNATexture1DImplementation(_device, width, genMipmap, format);
        }

        public Texture2DImplementation CreateTexture2DImplementation(int width, int height, bool genMipmap, SurfaceFormat format, DataBuffer data) {
            return new XNATexture2DImplementation(_device, width, height, genMipmap, format);
        }

        public Texture3DImplementation CreateTexture3DImplementation(int width, int height, int depth, bool genMipmap, SurfaceFormat format, DataBuffer data) {
            return new XNATexture3DImplementation(_device, width, height, depth, genMipmap, format);
        }

        public TextureCubeImplementation CreateTextureCubeImplementation(int size, bool genMipmap, SurfaceFormat format, DataBuffer[] data) {
            return new XNATextureCubeImplementation(_device, size, genMipmap, format);
        }

        public Texture2DImplementation CreateRenderTarget2DImplementation(int width, int height, bool genMipMaps, SurfaceFormat format, DepthFormat depthFormat, int multiSampleCount, RenderTargetUsage usage) {
            return new XNATexture2DImplementation(_device, width, height, genMipMaps, format, depthFormat, multiSampleCount, usage);
        }

        public TextureCubeImplementation CreateRenderTargetCubeImplementation(int size, bool genMipMaps, SurfaceFormat format, DepthFormat depthFormat, int multiSampleCount, RenderTargetUsage usage) {
            return new XNATextureCubeImplementation(_device, size, genMipMaps, format, depthFormat, multiSampleCount, usage);
        }
        public void Dispose() {
            //throw new NotImplementedException();
        }
    }
}
